package integerTree;

import java.util.Random;

public class TreeBuilder implements BinaryTreeBuilder {

    @Override
    public Tree TreeBuild(int[] inputIntegerArray, int rootNodeValue) throws Exception {
        if ((inputIntegerArray != null) && (rootNodeValue > 0)) {
            Tree tree = new Tree(rootNodeValue);

            for (int i = 0; i < inputIntegerArray.length; i++) {
                tree.insertNode(inputIntegerArray[i]);
            }

            return tree;
        } else throw new Exception("Input cannot be null or less.");
    }

    @Override
    public Tree RandomTreeBuild(int numberOfElements, int rootNodeValue) throws Exception {
        Random random = new Random();
        random.nextInt(Integer.MAX_VALUE);
        if ((numberOfElements >= 0) && (rootNodeValue > 0)) {
            Tree tree = new Tree(rootNodeValue);

            for (int i = 0; i < numberOfElements; i++) {
                tree.insertNode(Math.abs(random.nextInt()));
            }

            return tree;
        } else throw new Exception("Input cannot be null or less.");
    }

}
